
package todolist.logic.task;

import java.util.Comparator;
import todolist.entity.DateTimeEntity;
import todolist.entity.TaskEntity;

/**
 * Compares two Tasks by End Date, earliest Date first, later Dates second last, null Dates last.
 * @author Ivan
 */
public class EndDateAscending implements Comparator<TaskEntity> {
	
	private static final int FIRST_OBJECT_LESS = -1;
	private static final int FIRST_OBJECT_EQUAL = 0;
	private static final int FIRST_OBJECT_GREATER = 1;
	
	/**
	 * Returns -ve on Task 1 having smaller End Date, typically by natural ordering, 0 on same, and +ve on larger.
	 * Expects both Tasks to be non-null.
	 * @throws IllegalArgumentException
	 */
	@Override
	public int compare(TaskEntity pentTask1, TaskEntity pentTask2) {
		if (pentTask1 == null || pentTask2 == null)
			throw new IllegalArgumentException("Comparator requires 2 non-null tasks.");
		final DateTimeEntity entEndDate1 = pentTask1.getEndDT();
		final DateTimeEntity entEndDate2 = pentTask2.getEndDT();
		// handle null end dates, treat them as greatest
		if (entEndDate1 == null && entEndDate2 == null)
			return FIRST_OBJECT_EQUAL;
		if (entEndDate1 == null)
			return FIRST_OBJECT_GREATER;
		if (entEndDate2 == null)
			return FIRST_OBJECT_LESS;
		return entEndDate1.compareTo(entEndDate2);
	}
	
	/**
	 * Comparators of this class is never equal to any other Comparator.
	 */
	@Override
	public boolean equals(Object pobjData) {
		return false;
	}
	
}
